package com.taptech.doufu.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.BatteryManager;

import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.ui.view.theme.read.ReadTheme;
import com.taptech.doufu.ui.view.theme.read.ReadThemeHelper;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by longsiyang on 15/12/24.
 */
public class MobileState {

    static Context mContext;
    static MobileState instance;

    public String systemTime ;
    public int netWorkState ;
    public int battery;
    public boolean isCharger;

    BroadcastReceiver broadcastReceiver;

    private MobileState(){

    }

    static public MobileState getInstance(){
        mContext = WeMediaApplication.getInstance().getApplicationContext();
        if (instance == null){
            instance = new MobileState();
        }
        return instance;
    }


    public static int getBatteryResource(MobileState mobileState) {
        if(mobileState == null)
            return 0;
        int battery = mobileState.battery;
        int sourceId;
        ReadTheme.BatteryTheme batteryTheme = ReadThemeHelper.getReadTheme().getBatteryTheme();
        if(battery < 10){
            sourceId = batteryTheme.getBatteryOne();
        }else if(battery < 20){
            sourceId = batteryTheme.getBatteryTwo();
        }else if (battery < 40){
            sourceId = batteryTheme.getBatteryThree();
        } else if (battery < 70) {
            sourceId = batteryTheme.getBatteryFour();
        }else if(battery < 100){
            sourceId = batteryTheme.getBatteryFive();
        }else {
            sourceId = batteryTheme.getBatterySix();
        }
        return sourceId;
    }

    // 获取时间
    private String getSystemTime(){
        String time = null;
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        time = df.format(new Date());
        return time;
    }
    // 获取网络状态
    private int getSystemNetworkState(){
        int netType = 0;
        ConnectivityManager manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null){
            netType = networkInfo.getType();
        }
        return netType;
    }

    private int getBattery(){
        int battery = 0 ;
        BatteryManager batteryManager = (BatteryManager) mContext.getSystemService(Context.BATTERY_SERVICE);
        if (batteryManager != null){
            battery = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
        }

        return battery;
    }

    public void initListenMobileState(final StateChangeListener listener){
        systemTime = getSystemTime();
        netWorkState = getSystemNetworkState();
        battery = getBattery();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        intentFilter.addAction(Intent.ACTION_TIME_TICK);
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (action != null){
                    // 时间
                    if (action.equals(Intent.ACTION_TIME_TICK)){
                        systemTime = getSystemTime();
                    }
                    // 网络连接
                    else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)){
                        netWorkState = getSystemNetworkState();
                    }
                    // 电量
                    else if (action.equals(Intent.ACTION_BATTERY_CHANGED)){
                        int rawlevel = intent.getIntExtra("level", -1);
                        int scale = intent.getIntExtra("scale", -1);
                        int status=intent.getIntExtra("status",BatteryManager.BATTERY_STATUS_UNKNOWN);
                        if(status==BatteryManager.BATTERY_STATUS_CHARGING){
                            isCharger = true;
                        }else{
                            isCharger = false;
                        }
                        int level = -1; // percentage, or -1 for unknown
                        if (rawlevel >= 0 && scale > 0) {
                            level = (rawlevel * 100) / scale;
                        }
                        battery = level;
                    }
                    if (listener != null){
                        listener.onStateChanged();
                    }
                }
            }
        };
        mContext.registerReceiver(broadcastReceiver , intentFilter);
    }

    public interface StateChangeListener{
        void onStateChanged();

    };

    public String toString(){
        String str = null;




        return str;

    }

    public void destoryListner(){
        if (broadcastReceiver != null ){
            try {
                mContext.unregisterReceiver(broadcastReceiver);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }


}
